package com.mbbmap.security.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mbbmap.app.manager.ConfigManager;
import com.mbbmap.security.dao.SecUserDao;
import com.mbbmap.util.Constants;

public class ShowSecurityHome extends CommonDispatchAction {
	public ActionForward init(ActionMapping mapping,
		    ActionForm form, HttpServletRequest request,
		    HttpServletResponse response) throws Exception {

		    final String PROG_ID="ShowSecurityHome.init";

		    System.out.println(PROG_ID + " : BEGIN");
		    HttpSession session = request.getSession(false);
		    String nextPage = null;
		    
		  //check session condition
			if (session == null){
					return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
			}
			if(!isTokenValid(request)){
				System.out.println("Token validation failed!");
				session.invalidate();
				return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
			}else{
				resetToken(request);
				System.out.println("Token validation passed!");
			}

			// browser javasript is not enable
			if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
			    String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
			    session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
			    if (DEBUG) System.out.println("... Javascript not enable");
			    return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
			}
			String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
			SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
		
			String strFwd = "";
			if (oSecUserDao == null){
				strFwd ="sessionError";
			}
			else
			{
				
			}
			if (sessionstatus != null){
			    if (DEBUG) System.out.println("sessionstatus !=null");
				if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
					//This is from refresh button.. set session expires.
				    if (DEBUG) System.out.println("sessionstatus !=null 2");
		  		    String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
		  		    session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
					return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

				}
				
			}
			else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
				
				return mapping.findForward(strFwd);
		
			}
			else{
				
			}
		
			// LOGON
			try {

				    nextPage = Constants.KEY_ACTION_SUCCESS;			
					
			}catch (Exception e1) {
			    System.out.println("Exception: " + e1.toString());
				e1.printStackTrace();
				return mapping.findForward(Constants.KEY_ACTION_MAPPING_ERR);
			}
			finally{
				if(nextPage!=null){
					System.out.println(PROG_ID + " : END");
				    return mapping.findForward(nextPage);
				}
				
			}
			 return mapping.findForward(nextPage);
	}
}
